package com.example.dense_rack_server.controller;


import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dense_rack_server.entity.ArchivesArea;
import com.example.dense_rack_server.service.ArchivesAreaService;
import com.example.dense_rack_server.utils.ResponseModel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 档案区域 前端控制器
 * </p>
 *
 * @author 张彬
 * @since 2025-03-04
 */
@Controller
@RequestMapping("/archives/area")
public class ArchivesAreaController {

    @Resource
    public ArchivesAreaService archiveAreaService;
    /**
     * 档案区域页面
     */
    @GetMapping("/view")
    public String preview() {
        return "archivesArea/table-list";
    }

    /**
     * 绘制区域树
     */
    @GetMapping("/buildAreaTree")
    @ResponseBody
    public Map<String, Object> buildAreaTree() {
        return archiveAreaService.buildAreaTree();
    }

    /**
     * 档案区域新增页面
     */
    @GetMapping("/add")
    public String add() {
        return "archivesArea/addArea";
    }


    /**
     * 绘制区域树
     */
    @GetMapping("/buildAreaTree/table")
    @ResponseBody
    public ResponseModel buildAreaTreeTable() {
        return archiveAreaService.buildAreaTreeTable();
    }

    /**
     * 档案区域新增保存
     */
    @PostMapping("/save")
    @ResponseBody
    public ResponseModel saveArea(@RequestBody JSONObject data) {
        return archiveAreaService.saveArea(data);
    }

    /**
     * 判断下面是否还有子节点
     */
    @GetMapping("/has/child/{id}")
    @ResponseBody
    public ResponseModel hasChild(@PathVariable("id") String id) {
        String[] ids = id.split(",");
        for (String areaId : ids) {
            List<ArchivesArea> deptList = archiveAreaService.list(new QueryWrapper<ArchivesArea>().eq("parent_id", Convert.toInt(areaId)));
            if (deptList.size() > 0) {
                return ResponseModel.fail("区域有下级区域,不允许删除");
            }
        }
        return ResponseModel.success();
    }


    /**
     * 档案区域删除
     */
    @DeleteMapping("/delete/save/{id}")
    @ResponseBody
    public ResponseModel deleteArea(@PathVariable("id") String id) {
        return archiveAreaService.deleteArea(id);
    }


    /**
     * 更新页面
     * @param id
     * @return
     */
    @GetMapping("/update/{id}")
    public ModelAndView update(@PathVariable("id") String id) {
        ModelAndView modelAndView = new ModelAndView("archivesArea/editArea");
        ArchivesArea archivesArea = archiveAreaService.getById(id);
        modelAndView.addObject("archivesArea", archivesArea);
        return modelAndView;
    }

    /**
     * 区域修改
     */
    @PostMapping("/update/save")
    @ResponseBody
    public ResponseModel updateArea(@RequestBody  Map<String, Object> data) {
        return archiveAreaService.updateArea(data);
    }

    /**
     * 档案分类列表
     */
    @GetMapping("/list")
    @ResponseBody
    public Map<String, Object> areaList(@RequestParam(value = "typeName", required = false) String typeName) {
        return archiveAreaService.areaList();
    }
}
